﻿/// <reference path="jquery-1.9.1.js" />
/// <reference path="Core.js" />

$(function ()
{
    EntityName = EntityName.toLowerCase();

    core.Form.BindHeader(EntityName);
    //zw
    //添加中文日期格式模块
    $.fn.datetimepicker.dates['zh-CN'] = {
        days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"],
        daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六", "周日"],
        daysMin: ["日", "一", "二", "三", "四", "五", "六", "日"],
        months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthsShort: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        today: "当前时间",
        suffix: [],
        meridiem: ["上午", "下午"]
    };
    $('.form-time').datetimepicker({
        zIndex: 100,//自定义的 index
        language: 'zh-CN',
        format: 'yyyy-mm-dd hh:ii',
        weekStart: 1,
        autoclose: true
    });
    $('.form-date').datetimepicker({
        language: 'zh-CN',
        format: 'yyyy-mm-dd',
        autoclose: true,
        weekStart: 1,
        minView: 3
    });

    $("._FormContainer").scroll(function (e)
    {
        $('.datetimepicker').hide();
    })

    try
    {
        if ((typeof Privilege) != "undefined" && Privilege.WRITE > 0)
        {
            $("#Btn_Save").css('display', 'inline-block');
        }
        else
        {
            $("#Btn_Save").css('display', 'none');
        }
        if (!!Privilege && Privilege.CREATE > 0 && !XFormData)
        {
            $("#Btn_Save").css('display', 'inline-block');
        }
    }
    catch (e)
    {
        console.log(e);
    }


    //横向表头绑定
    $("#LookupListContainer-title").css({ "background": "#F5F6FA" });
    $("#LookupListContainer").scroll(function ()
    {
        var left = (-1) * $(this).scrollLeft();
        var top = $(this).scrollTop();
        $("#LookupListContainer-title").css("top", top);
    });
});
if (typeof (core) == 'undefined')
    core = {};




core.Form = {};



core.Form.BindHeader = function (entityname)
{
    var primaryAtt = "";
    $.each(EntityField, function (i, v)
    {
        if (v.IsPrimaryAtt)
        {
            primaryAtt = i;
            return false;
        }
    });
    if (FormId != "")
    {
        $("#pageHeader").text(entityname + ":" + XFormData[primaryAtt]);
    }
    else
    {
        $("#pageHeader").text("Create " + entityname);
    }
}

core.Form.ValueForm = function ()
{
    $.each(EntityField, function (i, v)
    {
        if (!v.IsNullable)
        {
            $("label[for=" + i + "]").append("<span style='color:#ff0000;'> *</span>");
        }
        switch (v.Type)
        {
            case "nvarchar":

            case "int":

            case "float":

            case "date":

            case "datetime":

            case "ntext":

            case "key":
                if (XFormData != null && XFormData[i] != null)
                    $("#" + i).val(XFormData[i]);
                break;
            case "picklist":
                if (XFormData != null)
                    $("#" + i).html(core.Util.CreateOptions(EntityOption[i], XFormData[i]));
                else
                    $("#" + i).html(core.Util.CreateOptions(EntityOption[i]));
                break;
            case "lookup":
                if (XFormData != null && XFormData[i] != null)
                {
                    $("#" + i).val(XFormData[i].name);
                    $("#" + i).attr("oid", XFormData[i].id);
                }
                break;
            case "bit":
                if (typeof (XFormData) != "undefined" && XFormData != null)
                    $("#" + i).html(core.Util.CreateRadios(i, [{ Value: 0, Text: '否' }, { Value: 1, Text: '是' }], XFormData[i]));
                else
                    $("#" + i).html(core.Util.CreateRadios(i, [{ Value: 0, Text: '否' }, { Value: 1, Text: '是' }]));
        }
    })
}


core.Form.BindDef = function ()
{
    if (typeof (__Def) != 'undefined' && !!__Def)
    {
        def = JSON.parse(decodeURIComponent(__Def));
        for (var i in def)
        {
            if (typeof (def[i]) == "object")
            {
                $("#" + i).val(def[i].value);
                $("#" + i).attr("oid", def[i].oid);
            }
            else
            {
                $("#" + i).val(def[i])
            }
        }
    }
}

core.Form.GetEntityData = function ()
{
    var changedData = {};

    var IsCreateForm = true;
    if (FormId != "")
        IsCreateForm = false;


    $("[attname]").each(function (i, v)
    {
        $this = $(this);
        var attname = $this.attr("attname");
        switch ($this.attr("atttype"))
        {
            case "key":
                break;
            case "nvarchar":
            case "date":
            case "datetime":
            case "password":
            case "ntext":


                var value = $this.val() == "" ? null : $this.val();
                if (!IsCreateForm)
                {
                    if (value != XFormData[attname])
                    {
                        XFormData[attname] = value;
                        changedData[attname] = value;
                    }
                }
                else
                {
                    changedData[attname] = value;
                }
                break;
            case "int":
            case "picklist":
                var value = $this.val() == "" ? null : $this.val();
                if (!IsCreateForm)
                {
                    if (value != XFormData[attname])
                    {
                        XFormData[attname] = (value == null ? null : parseInt(value));
                        changedData[attname] = (value == null ? null : parseInt(value));
                    }
                }
                else
                {
                    changedData[attname] = (value == null ? null : parseInt(value));
                }
                break;
            case "float":
                var value = $this.val() == "" ? null : $this.val();
                if (!IsCreateForm)
                {
                    if (value != XFormData[attname])
                    {
                        XFormData[attname] = (value == null ? null : parseFloat(value));
                        changedData[attname] = (value == null ? null : parseFloat(value));
                    }
                }
                else
                {
                    changedData[attname] = (value == null ? null : parseFloat(value));
                }
                break;
            case "bit":
                var value;
                if ($('input:radio[name=' + attname + ']:checked').length == 0)
                {
                    value = null;
                }
                else
                {
                    value = ($('input:radio[name=' + attname + ']:checked').val() == "1");
                }

                if (!IsCreateForm)
                {
                    if (value != XFormData[attname])
                    {
                        XFormData[attname] = value;
                        changedData[attname] = value;
                    }
                } else
                {
                    changedData[attname] = value;
                }
                break;
            case "lookup":
                var lookup = null;
                if ($this.attr("oid") != "")
                {
                    lookup = {};
                    lookup["id"] = $this.attr("oid").toLocaleLowerCase();
                    lookup["entityname"] = $this.attr("etn");
                    lookup["name"] = $this.val();
                }
                if (!IsCreateForm)
                {
                    if (lookup != null)
                    {
                        if (lookup["id"] != (XFormData[attname] == null ? null : XFormData[attname].Id))
                        {
                            XFormData[attname] = lookup;
                            changedData[attname] = lookup;
                        }
                    }
                    else
                    {
                        if (XFormData[attname] != null)
                        {
                            XFormData[attname] = lookup;
                            changedData[attname] = lookup;
                        }
                    }
                }
                else
                {
                    changedData[attname] = lookup;
                }
                break;
        }
    });
    return changedData;
}



core.Form.IntReq = /^-?\d+$/;
core.Form.FloatReq = /^-?([1-9]\d*|[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$/;
core.Form.CheckFloatInput = function (src)
{
    if ($(src).val() != "")
    {
        if (!core.Form.FloatReq.test($(src).val()))
        {
            $(src).parent().addClass("has-error");
            $(src).focus();
        }
        else
        {
            $(src).parent().removeClass("has-error");
        }
    }
    else
    {
        $(src).parent().removeClass("has-error");
    }
}

core.Form.CheckIntInput = function (src)
{
    if ($(src).val() != "")
    {
        if (!core.Form.IntReq.test($(src).val()))
        {
            $(src).parent().addClass("has-error");
            $(src).focus();
        }
        else
        {
            $(src).parent().removeClass("has-error");
        }
    } else
    {
        $(src).parent().removeClass("has-error");
    }
}
//zw
$(document).on('click', "div[id='LookupListContainer-body'] tbody tr", function (e)
{
    e = e || window.event;
    var target = e.target || e.srcElement;
    var checkBox = $(this).find('td:first').find('input:first');
    if (checkBox.prop('checked') == true)
    {
        checkBox.prop('checked', false);

        $(this).css('background-color', '#fff');
    }
    else
    {
        var OcheckBox = $(this).siblings('tr').find('td:first').find('input').prop('checked', false);
        $(this).siblings('tr').css('background-color', '#fff');
        checkBox.prop('checked', true);
        $(this).css('background-color', '#CCE8FF');
    }
    checkBox.change();
});
$(document).on('click', "div[id='LookupListContainer-body'] tbody tr td input", function (e)
{
    e = e || window.event;
    var target = e.target || e.srcElement;
    if ($(this).prop('checked') == true)
    {
        $(this).prop('checked', false);
        $(this).parent('td').parent('tr').css('background-color', '#fff');
    }
    else
    {
        var OcheckBox = $(this).parent('td').parent('tr').siblings('tr').find('td:first').find('input').prop('checked', false);
        $(this).parent('td').parent('tr').siblings('tr').css('background-color', '#fff');
        $(this).prop('checked', true);
        $(this).parent('td').parent('tr').css('background-color', '#CCE8FF');
    }

});



